package com.uchedao.vehicledetection.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapTool {

	public static Bitmap zoom(Bitmap bitmap, float zf) {
		Matrix matrix = new Matrix();
		matrix.postScale(zf, zf);
		return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
	}

	public static Bitmap zoom(Bitmap bitmap, float wf, float hf) {
		Matrix matrix = new Matrix();
		matrix.postScale(wf, hf);
		return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
	}

	public static Bitmap getRCB(Bitmap bitmap, float roundPX) {
		// RCB means
		// Rounded
		// Corner Bitmap
		Bitmap dstbmp = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(dstbmp);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPX, roundPX, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return dstbmp;
	}
	
	public static int dp2px(Context context, float dp)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dp * scale + 0.5f);
	}
	
	public static int px2dp(Context context, float px)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}
	
	public static Bitmap getCircularBitmap(Bitmap sourceBitmap){
		int targetWidth = sourceBitmap.getWidth();
		int targetHeight = sourceBitmap.getHeight();
		Bitmap targetBitmap = Bitmap.createBitmap(
		targetWidth,
		targetHeight,
		Config.ARGB_8888);
		Canvas canvas = new Canvas(targetBitmap);
		Path path = new Path();
		path.addCircle(
		((float)targetWidth - 1) / 2,
		((float)targetHeight - 1) / 2,
		(Math.min(((float)targetWidth), ((float)targetHeight)) / 2),
		Path.Direction.CCW);
		canvas.clipPath(path);
		canvas.drawBitmap(
		sourceBitmap,
		new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
		new Rect(0, 0, targetWidth, targetHeight),
		null);
	
		return targetBitmap;

	}
	
	public static Bitmap bytes2Bitmap(byte[] b) {
		if(b.length != 0)
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		else
			return null;
	}
	
	public static byte[] Bitmap2Bytes(Bitmap img) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		float scale = Math.min(1, Math.min(600f / img.getWidth(), 600f / img.getHeight()));
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);

		Bitmap imgSmall = Bitmap.createBitmap(img, 0, 0, img.getWidth(),
				img.getHeight(), matrix, false);
		// Log.v(TAG, "imgSmall size : " + imgSmall.getWidth() + " " +
		// imgSmall.getHeight());

		imgSmall.compress(Bitmap.CompressFormat.PNG, 100, stream);
		byte[] array = stream.toByteArray();
		return array;
	}
	
	public static File Bitmap2File(Context context, Bitmap img) {
		File file = null;
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		try {
			file = new File(context.getFilesDir()+"/temp");
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(Bitmap2Bytes(img));
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if(fos != null)
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return file;
	}
	
	public static int getScreenWidthPX(Context context) {
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return metrics.widthPixels;
	}
	
	public static int getScreenHeightPX(Context context) {
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return metrics.heightPixels;
	}
	
	public static Bitmap File2Bitmap(Context context, File f) {
		return compressBitmapFromFile(context, f.getAbsolutePath());
	}
	
	public static Drawable Bitmap2Drawable(Context context, Bitmap bm) {
		return new BitmapDrawable(context.getResources(), bm);
	}
	
	public static Bitmap compressBitmapFromFile(Context context, String srcPath) {
		BitmapFactory.Options newOp = new BitmapFactory.Options();
		newOp.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOp);
		
		newOp.inJustDecodeBounds = false;
		int w = newOp.outWidth;
		int h = newOp.outHeight;
		int ww = getScreenWidthPX(context);
		int hh = getScreenHeightPX(context);
		int be = 1;
		if(w>h && w>ww)
			be = w / ww;
		else if(w<h && h>hh)
			be = h / hh;
		newOp.inSampleSize = be;
		newOp.inPreferredConfig = Config.ARGB_8888;
		newOp.inPurgeable = true;
		newOp.inInputShareable = true;
		return BitmapFactory.decodeFile(srcPath, newOp);
	}
}
